# -*- coding: utf-8 -*-
# $Id: group.py 25 2009-02-15 03:19:36Z gkuznets $

# Copyright 2008, 2009 Grigoriy Kuznetsov (gkuznets@yandex.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Group(object):
    """Группа проверки по какой-либо задаче"""
    gdict = {}
  
    def __init__(self, index):
        # Номер группы
        self.index = index
        # Список проверяемых в данный момент пачек работ
        self.blist = []
        # Число проверенных работ по каждой из проверок
        self.papers_complete = [0, 0]
        # Ожидаемое максимальное время проверки пачки в секундах
        self.max_check_time = 15*60
        Group.__addGroup(self)
  
    def addBatch(self, batch):
        assert(batch not in self.blist)
        self.blist.append(batch)
    
    def takeBatch(self, batch):
        self.blist.remove(batch)
    
    @staticmethod
    def __addGroup(group):
        Group.gdict[group.index] = group
    
    @staticmethod
    def find(index):
        return Group.gdict[index]
    
    @staticmethod
    def all():
        return Group.gdict.values()
    
    def progress(self, cnum=None):
        from batch import Batch
        value = 0
        if cnum is not None:
            if Batch.total_papers > 0:
                value = (100*self.papers_complete[cnum])/Batch.total_papers
        else:
            value = (self.progress(0) + self.progress(1)) / 2
        return value
